package diva.pap.widget;

import diva.pap.model.Application;

import diva.android_library.rpc.Response;

import static diva.lib.lang.Lang.info;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.view.View;
import android.widget.RemoteViews;
import diva.android_library.lang.AndroidLang;
import diva.pap.LoginActivity;
import diva.pap.R;

public class WidgetUpdateService extends Service {

  private final Application application = Application.getInstance();

  public WidgetUpdateService() {
    AndroidLang.initialize();
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    info("starting");
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
    registerRefreshButtonClick(remoteViews);
    registerLaunchApplication(remoteViews);
    updateDisplay(remoteViews);
    ComponentName widgetComponentName = new ComponentName(this, WidgetProvider.class);
    AppWidgetManager manager = AppWidgetManager.getInstance(this);
    manager.updateAppWidget(widgetComponentName, remoteViews);
    info("end");
  }

  private void registerLaunchApplication(RemoteViews remoteViews) {
    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    ComponentName componentName = new ComponentName(this, LoginActivity.class);
    intent.setComponent(componentName);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    remoteViews.setOnClickPendingIntent(R.id.widget, pendingIntent);
  }

  private void registerRefreshButtonClick(RemoteViews remoteViews) {
    Intent intent = new Intent(this, WidgetUpdateService.class);
    PendingIntent pendingIntent =
        PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.widget_refresh_button, pendingIntent);
  }

  private void showData(RemoteViews remoteViews, String click, String transaction) {
    remoteViews.setTextViewText(R.id.click_value, click);
    remoteViews.setTextViewText(R.id.transaction_value, transaction);
    remoteViews.setViewVisibility(R.id.message_widget, View.GONE);
    remoteViews.setViewVisibility(R.id.info_panel, View.VISIBLE);
  }

  private void showMessage(int resource_id, RemoteViews remoteViews) {
    remoteViews.setTextViewText(R.id.message_widget, getString(resource_id));
    remoteViews.setViewVisibility(R.id.message_widget, View.VISIBLE);
    remoteViews.setViewVisibility(R.id.info_panel, View.GONE);
  }

  private void updateDisplay(final RemoteViews remoteViews) {
    info("updateDisplay start");
    if (application.isSetup())
      new WidgetDataRequest(new Response<WidgetDataRequest>() {
        public void onReuestDone(WidgetDataRequest data) {
          if (data.isSuccess())
            showData(remoteViews, data.getClicksCount(), data.getTransactionsCount());
          else showMessage(R.string.widgetmsg_notsetup, remoteViews);
        }

        public void onReuestFailed() {
          showMessage(R.string.widgetmsg_error_feching_data, remoteViews);
        }
      }).send();
    else showMessage(R.string.widgetmsg_notsetup, remoteViews);
    info("updateDisplay end");
  }
}
